<?php
/**
 *
 * @author 丁泓懿
 * @package 一个面向对象的框架
 */

header('X-dinghongyiFrameWork: v1.0 Beta');

/*设置环境路径*/
set_include_path(
	get_include_path() .
	PATH_SEPARATOR . app_path.'/controller'.
	PATH_SEPARATOR. app_path.'/model'.
	PATH_SEPARATOR.'./'
);
require_once 'dingwork/model/Exception.php';
require_once 'dingwork/function.extends.php';
interface AppInterface{
	public static function run();
	public function initenv();
	public function initRegsiter();
	public function initConfig();
	public function initDb();
	public function initView();
	public function initRouter();
	public function initParam();
	public function initController();
}
class App implements AppInterface{
	private static $_ins;
	private $_regsiter = null;
	private $superglobal = array(
		'GLOBALS' => 1,
		'_GET' => 1,
		'_POST' => 1,
		'_REQUEST' => 1,
		'_COOKIE' => 1,
		'_SERVER' => 1,
		'_ENV' => 1,
		'_FILES' => 1,
	);
	private function __construct(){
		$this->initenv();
		$this->initRegsiter();
		$this->initConfig();
		$this->initDb();
		$this->initView();
		$this->initRouter();
		$this->initParam();
		$this->initController();

	}
	public function initenv(){
		if(PHP_VERSION < '5.3.0') {
			//exit('此系统不支持5.3.0以下的PHP');
			set_magic_quotes_runtime(0);
		}
		define('MAGIC_QUOTES_GPC', function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc());
		foreach ($GLOBALS as $key => $value) {
			if (!isset($this->superglobal[$key])) {
				$GLOBALS[$key] = null; unset($GLOBALS[$key]);
			}
		}
		if (isset($_GET['GLOBALS']) ||isset($_POST['GLOBALS']) ||  isset($_COOKIE['GLOBALS']) || isset($_FILES['GLOBALS'])) {
			exit('request_tainting');
		}
		if(!MAGIC_QUOTES_GPC) {
			$_GET		= daddslashes($_GET, 1, TRUE);
			$_POST		= daddslashes($_POST, 1, TRUE);
			$_COOKIE	= daddslashes($_COOKIE, 1, TRUE);
			$_SERVER	= daddslashes($_SERVER);
			$_REQUEST	= daddslashes($_REQUEST, 1, TRUE);
		}
		$_FILES		= daddslashes($_FILES);
		if($_SERVER['REQUEST_METHOD'] == 'GET' && !empty($_SERVER['REQUEST_URI'])) {
			$this->_xss_check();
		}
	}
	private function _xss_check() {
		$temp = strtoupper(urldecode(urldecode($_SERVER['REQUEST_URI'])));
		if(strpos($temp, '<') !== false || strpos($temp, '"') !== false || strpos($temp, 'CONTENT-TRANSFER-ENCODING') !== false) {
			exit('request_tainting');
		}
		return true;
	}
	public function initRegsiter(){
		require_once 'dingwork/model/Register.php';
		$this->_regsiter = Regsiter::getInterface();
		$this->_regsiter->set('initRegsiter',true);
	}
	public function initConfig(){
		require_once 'dingwork/model/Config.php';
		$Config = new Config();
		$ConfigArray = $Config->getConfig();
		$this->_regsiter->set('Config',$ConfigArray);
		$this->_regsiter->set('initConfig',true);
	}
	public function initDb(){
		require_once 'dingwork/model/Db.php';
		$config = $this->_regsiter->get('Config');
		if(!empty($config['DB_TYPE'])){
			$Db = Db::facetory($config['DB_TYPE'],$config);
			$this->_regsiter->set('Db',$Db);
			$this->_regsiter->set('initDb',true);
		}
	}
	public function initView(){
		require_once 'dingwork/model/View.php';
		$View = new View();
		$this->_regsiter->set('View',$View);
		$this->_regsiter->set('initView',true);
	}
	public function initRouter(){
		require_once 'dingwork/model/Router.php';
		$Router = new Router($this->_regsiter->get('Config'));
		$this->_regsiter->set('Router',$Router);
		$this->_regsiter->set('initRouter',true);
	}
	public function initParam(){
		require_once 'dingwork/model/Param.php';
		$Param = new Param();
		$this->_regsiter->set('Param',$Param);
		$this->_regsiter->set('initParam',true);
	}
	public function initController(){
		require_once 'dingwork/model/Controller.php';
		$Controller = new Controller();
		$this->_regsiter->set('initController',true);
	}
	public static function run(){
		set_exception_handler(array('DoException','exception'));
		set_error_handler(array('DoException',"error"));
		return new App();
	}
}
